package com.camel.studyApp.genericapplicationcontext;

import com.camel.studyApp.context.Bean3;
import com.camel.studyApp.context.Bean4;
import com.camel.studyApp.springboot.LifeCycleBean;
import com.camel.studyApp.springboot.LifeCycleBean2;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

public class TestSpringboot02 {

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();

        context.registerBean("lifeCycleBean", LifeCycleBean.class);
        context.registerBean("lifeCycleBean2", LifeCycleBean2.class);
        context.registerBean("bean5", Bean5.class);
        context.registerBean(Config.class);
        DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
        beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);// @Autowired 注解 @Value
        context.registerBean(CommonAnnotationBeanPostProcessor.class); // @Resource  @PostContruct @PreDestroy
        ConfigurationPropertiesBindingPostProcessor.register(beanFactory); // 解析ConfigurationProperties
        context.registerBean(ConfigurationClassPostProcessor.class); // @ComponentScan @Bean @Iport @ImportResource
        context.registerBean(MapperScannerConfigurer.class, beanDefinition -> {
            beanDefinition.getPropertyValues().add("basePackage", "com.camel.studyApp.mapper");
        });
        context.refresh(); //执行BeanFactory后处理器 , 添加bean置处理器 ， 初始化所有单例

        Object bean5 = context.getBean("bean5");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }

        System.out.println(context.getBean(Bean5.class));

        context.close();
    }
}
